<?php
	if(!defined("AUCUNE_ERREUR"))
		define("AUCUNE_ERREUR", 0);
	
	if(!defined("ERREUR_TAG_EXISTE_DEJA"))
		define("ERREUR_TAG_EXISTE_DEJA", 1);
	
	if(!defined("ERREUR_TAG_VIDE"))
		define("ERREUR_TAG_VIDE", 2);
	
	if(!defined("ERREUR_USAGER_VIDE"))
		define("ERREUR_USAGER_VIDE", 1);

	function get_file_info($file_id){
		$sql = "select * from file where id = $file_id";
		$req = mysql_query($sql) or die($sql.'<br>'.mysql_error());
		$enr = mysql_fetch_assoc($req);
		$ext = $enr['ext'];
		$icon = "";
		$contentType = "";
		switch ($ext){
			case ($ext=="jpg" || $ext=="jpeg" || $ext=="gif" || $ext=="png"):
				$icon = "images/image.png";
				if($ext=="jpg" || $ext=="jpeg")
					$contentType = "image/jpeg";
				else if($ext=="gif")
					$contentType = "image/gif";
				else if($ext=="png")
					$contentType = "image/x-png";
				break;
			case ($ext=="pdf"):
				$icon = "images/pdf.png";
				$contentType = "application/x-pdf";
				break;
			case ($ext=="doc" || $ext=="docx" || $ext=="txt" || $ext=="rtf"):
				$icon = "images/text.png";
				if($ext=="doc" || $ext=="docx")
					$contentType = "application/msword";
				else if($ext=="txt")
					$contentType = "text/plain";
				else if($ext=="rtf")
					$contentType = "application/rtf";
				break;
			default:
				$icon = "images/file.png";
				$contentType = "";
				break;
		}
		return Array("id"=>$enr['id'],"name"=>$enr['name'],"file"=>$enr['file'],"ext"=>$enr['ext'],"icon"=>$icon,
				"contentType"=>$contentType);
	}
	
	function get_tag_files($label_id){
		$results = Array();
		$sql = "select file_id from file_label where label_id=$label_id";
		$req = mysql_query($sql) or die($sql.'<br>'.mysql_error());
		while($enr = mysql_fetch_assoc($req)){
			array_push($results,get_file_info($enr['file_id']));
		}
		
	}
	
	function get_user_files($user_id){
		$sql = "select id from label where name='All' and user_id=".$_SESSION['user_id'];
		$req = mysql_query($req) or die($sql.'<br>'.mysql_error());
		$enr = mysql_fetch_assoc($req);
		return get_tag_file($enr['id']);
	}
	
	function get_current_user_files(){
		return get_user_files($_SESSION['user_id']);
	}
?>